﻿using UnityEngine;
using System.Collections;

public class VictoryPhoto : MonoBehaviour 
{

	RenderTexture						mRT = null;
	Texture								mTex = null;
	bool								mNeedCut = false;
	
	public Camera						TargetCameraLink = null;
	public UITexture					VictoryPhotoTextureLink = null;
	public GameObject					TestPlaneLink = null;
	public GameObject					ScreenQuadLink = null;

	// Use this for initialization
	void Start () 
	{
	
	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}

	public void Capture()
	{

		this.StartCoroutine ( this.TestCapture () );
		
	}

	void OnGUI()
	{

		if( GUI.Button( new Rect( 300, 300, 100, 80 ), "Test" ) == true )
		{
			//this.StartCoroutine( this.Test() );
			//this.Test2();
			//this.StartCoroutine( this.Test2() );
			//this.TestCapture();
			this.StartCoroutine( this.TestCapture() );
			//this.StartCoroutine( this.TestA() );
			//this.TestA();
			//this.TestCapture();
		}

		if( this.mTex != null )
		{
			//GUI.DrawTexture( new Rect( 100, 100, 300, 300 ), this.mTex );
		}
	}


	/*
	IEnumerator Test2()
	{

		if( this.mCamera == null )
		{
			this.mCamera = this.gameObject.AddComponent( typeof( Camera ) ) as Camera;
			this.mCamera.backgroundColor = Camera.main.backgroundColor;
			this.mCamera.clearFlags = Camera.main.clearFlags;
			this.mCamera.orthographicSize = Camera.main.orthographicSize;
			this.mCamera.orthographic = Camera.main.orthographic;
			this.mCamera.nearClipPlane = Camera.main.nearClipPlane;
			this.mCamera.farClipPlane = Camera.main.farClipPlane;
			this.mCamera.cullingMask = Camera.main.cullingMask;
			this.mCamera.hdr = Camera.main.hdr;
			this.mCamera.useOcclusionCulling = Camera.main.useOcclusionCulling;
			this.mCamera.rect = Camera.main.rect;

			this.mRT = new RenderTexture( Screen.width, Screen.height, 24 );
			this.mCamera.targetTexture = this.mRT;
			this.mCamera.depth = Camera.main.depth - 1;

		}
		this.mCamera.transform.position = Camera.main.transform.position;
		this.mCamera.transform.rotation = Camera.main.transform.rotation;	

		
		
		yield return null;
		//yield return new WaitForEndOfFrame();
		
		//this.MakeToTexture();
		//this.StartCoroutine( this.MakeToTexture() );
	}
	*/
	/*
	//IEnumerator TestA()
	void TestA()
	{
		//yield return new WaitForEndOfFrame();
		RenderTexture rt = new RenderTexture( 256, 256, 24 );
		this.mTex = new Texture2D( 256, 256, TextureFormat.RGB24, false );
		//this.camera.targetTexture = rt;
		Camera.main.targetTexture = rt;
		RenderTexture.active = rt;
		
		//this.camera.Render();
		Camera.main.Render();
		

		//yield return new WaitForEndOfFrame ();
		this.mTex.ReadPixels( new Rect( 0, 0, 256, 256 ), 0, 0 );

		//yield return new WaitForEndOfFrame ();
		RenderTexture.active = null;
		//this.camera.targetTexture = null;
		Camera.main.targetTexture = null;
		RenderTexture.Destroy( rt );
		//yield return 0;
		
	}
	*/


	//void TestCapture()
	IEnumerator TestCapture()
	{
		this.mRT = new RenderTexture( Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32 );
		this.mRT.Create();
		
		//Camera.main.targetTexture = this.mRT;
		this.TargetCameraLink.targetTexture = this.mRT;
		
		
		RenderTexture.active = this.mRT;
		

		//Camera.main.Render();
		this.TargetCameraLink.Render();

		//yield return new WaitForEndOfFrame();
		
		this.mTex = this.mRT;


		/*
		this.mTex = new Texture2D( Screen.width, Screen.height );
		Texture2D t2d = this.mTex as Texture2D;		
		t2d.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0 );
		byte[] bt_array = t2d.EncodeToPNG();
		Texture2D new_t2d = new Texture2D( Screen.width, Screen.height );
		new_t2d.LoadImage( bt_array );
		//new_t2d.LoadRawTextureData( bt_array );
		new_t2d.Apply();
		*/

		
		
		if( this.mTex == null )
		{
			Debug.Log( "NNNN" );
		}
		else
		{			
			Debug.Log( "Width : " + this.mTex.width );			
			//this.TestPlaneLink.renderer.material.mainTexture = this.mTex;
			//this.VictoryPhotoTextureLink.mainTexture = this.TestPlaneLink.renderer.material.mainTexture as Texture2D;
			//this.VictoryPhotoTextureLink.mainTexture = new_t2d;
			//this.VictoryPhotoTextureLink.Update();		
			this.ScreenQuadLink.renderer.enabled = true;
			ScreenQuad s = this.ScreenQuadLink.GetComponent ( typeof ( ScreenQuad ) ) as ScreenQuad;
			s.Build();
			this.ScreenQuadLink.renderer.material.mainTexture = this.mTex;
		}

		//Camera.main.targetTexture = null;
		this.TargetCameraLink.targetTexture = null;
		RenderTexture.active = null;
		
		yield return null;
	}


	/*
	IEnumerator OnRenderImage( RenderTexture src, RenderTexture dest )
	{

		RenderTexture.active = src;		

		Graphics.Blit( src, dest );

		yield return new WaitForEndOfFrame ();
		this.mTex = new Texture2D ( Screen.width, Screen.height, TextureFormat.ARGB32, false );
		this.mTex.ReadPixels ( new Rect ( 0, 0, Screen.width, Screen.height ), 0, 0 );
		this.mTex.Apply ();

		RenderTexture.active = null;
		
	}
	*/

	IEnumerator ReadAAA()
	{
		yield return new WaitForEndOfFrame ();
		
	}
	


	/*
	IEnumerator MakeToTexture()
	{

		RenderTexture.active = this.mRT;

		this.mTex = new Texture2D ( Screen.width, Screen.height, TextureFormat.ARGB32, false );

		
		this.mTex.ReadPixels ( new Rect ( 0, 0, Screen.width, Screen.height ), 0, 0 );
		yield return new WaitForEndOfFrame ();
		this.mTex.Apply ();		
		RenderTexture.active = null;		
		
	}
	*/


	void OnPostRender()
	{

		//this.MakeToTexture();
		


	}




	//	카메라에 장착한 경우
	
	IEnumerator Test()
	{
		
		//UIRoot uiRoot = GameObject.FindObjectOfType( typeof( UIRoot ) ) as UIRoot;
		//uiRoot.gameObject.SetActive( false );
		
		
		RenderTexture rt = new RenderTexture( Screen.width, Screen.height, 24 );
		this.mTex = new Texture2D ( Screen.width, Screen.height, TextureFormat.ARGB32, false );			
		
		Camera.main.targetTexture = rt;
		//this.camera.targetTexture = rt;
		//this.mCamera.targetTexture = rt;

		RenderTexture.active = rt;		
		
		Camera.main.Render();
		//this.camera.Render();
		//this.mCamera.Render();

		

		yield return new WaitForEndOfFrame ();
		//yield return new WaitForFixedUpdate();
		//this.mTex.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0 );	
		//this.mTex.Apply( false, true );		
		
		Camera.main.targetTexture = null;
		//this.camera.targetTexture = null;
		//this.mCamera.targetTexture = null;
		
		
		RenderTexture.active = null;
		//uiRoot.gameObject.SetActive ( true );

	}


	Mesh			_mesh = null;
	public int meshCellWidth = 16;
	public int meshCellHeight = 16;
	//============================================================
	private void CreateMesh()
	{
		int xLoopCount;
		int yLoopCount;
		int numTri;
		int numVert;
		float umul;
		float vmul;
		Vector2[] uv;
		Vector3[] verts;
		Color32[] vcolors;
		int[] tris;
		int loopx;
		int loopy;
		int index;

		if ( meshCellWidth < 1 )
			meshCellWidth = 1;

		if ( meshCellWidth > 128 )
			meshCellWidth = 128;

		if ( meshCellHeight < 1 )
			meshCellHeight = 1;

		if ( meshCellHeight > 128 )
			meshCellHeight = 128;

		xLoopCount = meshCellWidth + 1;
		yLoopCount = meshCellHeight + 1;

		numVert = xLoopCount * yLoopCount;
		numTri = meshCellWidth * meshCellHeight * 6;

		umul = 1.0f / meshCellWidth;
		vmul = 1.0f / meshCellHeight;

		uv = new Vector2[ numVert ];
		verts = new Vector3[ numVert ];
		vcolors = new Color32[ numVert ];
		tris = new int[ numTri ];

		index = 0;
		for ( loopy = 0; loopy < yLoopCount; loopy++ )
		{
			for ( loopx = 0; loopx < xLoopCount; loopx++ )
			{
				verts[ index ] = new Vector3 ( loopx * umul, loopy * vmul, 0.0f );
				uv[ index ] = new Vector2 ( ( float )loopx * umul, ( float )loopy * vmul );

				index++;
			}
		}

		index = 0;
		for ( loopy = 0; loopy < meshCellHeight; loopy++ )
		{
			for ( loopx = 0; loopx < meshCellWidth; loopx++ )
			{
				tris[ index ] = loopx + ( loopy * xLoopCount );
				index++;

				tris[ index ] = loopx + ( ( loopy + 1 ) * xLoopCount );
				index++;

				tris[ index ] = loopx + 1 + ( loopy * xLoopCount );
				index++;

				tris[ index ] = loopx + ( ( loopy + 1 ) * xLoopCount );
				index++;

				tris[ index ] = loopx + 1 + ( ( loopy + 1 ) * xLoopCount );
				index++;

				tris[ index ] = loopx + 1 + ( loopy * xLoopCount );
				index++;
			}
		}

		_mesh = new Mesh ();

		_mesh.vertices = verts;
		_mesh.triangles = tris;
		_mesh.colors32 = vcolors;
		_mesh.uv = uv;
		_mesh.RecalculateNormals ();
		_mesh.Optimize ();
		_mesh.MarkDynamic ();

		//_meshColor = _mesh.colors32;

		return;
	}

}
